<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>rangeset_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_rangeset_test.js.html">rangeset_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.math.RangeSetTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.math.RangeSetTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.iter&#39;);
<a name="line19"></a>goog.require(&#39;goog.math.Range&#39;);
<a name="line20"></a>goog.require(&#39;goog.math.RangeSet&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>/**
<a name="line25"></a> * Produce legible assertion results for comparing ranges. The expected range
<a name="line26"></a> * may be defined as a goog.math.Range or as a two-element array of numbers. If
<a name="line27"></a> * two ranges are not equal, the error message will be in the format:
<a name="line28"></a> * &quot;Expected &lt;[1, 2]&gt; (Object) but was &lt;[3, 4]&gt; (Object)&quot;
<a name="line29"></a> *
<a name="line30"></a> * @param {!goog.math.Range|!Array.&lt;number&gt;|string} a A descriptive string or
<a name="line31"></a> *     the expected range.
<a name="line32"></a> * @param {!goog.math.Range|!Array.&lt;number&gt;} b The expected range when a
<a name="line33"></a> *     descriptive string is present, or the range to compare.
<a name="line34"></a> * @param {goog.math.Range=} opt_c The range to compare when a descriptive
<a name="line35"></a> *     string is present.
<a name="line36"></a> */
<a name="line37"></a>function assertRangesEqual(a, b, opt_c) {
<a name="line38"></a>  var message = opt_c ? a : &#39;&#39;;
<a name="line39"></a>  var expected = opt_c ? b : a;
<a name="line40"></a>  var actual = opt_c ? opt_c : b;
<a name="line41"></a>
<a name="line42"></a>  if (goog.isArray(expected)) {
<a name="line43"></a>    assertEquals(message + &#39;\n&#39; +
<a name="line44"></a>                 &#39;Expected ranges must be specified as goog.math.Range &#39; +
<a name="line45"></a>                 &#39;objects or as 2-element number arrays. Found [&#39; +
<a name="line46"></a>                 expected.join(&#39;, &#39;) + &#39;]&#39;,
<a name="line47"></a>                 2, expected.length);
<a name="line48"></a>    expected = new goog.math.Range(expected[0], expected[1]);
<a name="line49"></a>  }
<a name="line50"></a>
<a name="line51"></a>  if (!goog.math.Range.equals(/** @type {!goog.math.Range} */ (expected),
<a name="line52"></a>                              /** @type {!goog.math.Range} */ (actual))) {
<a name="line53"></a>    if (message) {
<a name="line54"></a>      assertEquals(message, expected, actual);
<a name="line55"></a>    } else {
<a name="line56"></a>      assertEquals(expected, actual);
<a name="line57"></a>    }
<a name="line58"></a>  }
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Produce legible assertion results for comparing two lists of ranges. Expected
<a name="line64"></a> * lists may be specified as a list of goog.math.Ranges, or as a list of
<a name="line65"></a> * two-element arrays of numbers.
<a name="line66"></a> *
<a name="line67"></a> * @param {Array.&lt;goog.math.Range|Array.&lt;number&gt;&gt;|string} a A help
<a name="line68"></a> *     string or the list of expected ranges.
<a name="line69"></a> * @param {Array.&lt;goog.math.Range|Array.&lt;number&gt;&gt;} b The list of
<a name="line70"></a> *     expected ranges when a descriptive string is present, or the list of
<a name="line71"></a> *     ranges to compare.
<a name="line72"></a> * @param {Array.&lt;goog.math.Range&gt;=} opt_c The list of ranges to compare when a
<a name="line73"></a> *     descriptive string is present.
<a name="line74"></a> */
<a name="line75"></a>function assertRangeListsEqual(a, b, opt_c) {
<a name="line76"></a>  var message = opt_c ? a + &#39;\n&#39; : &#39;&#39;;
<a name="line77"></a>  var expected = opt_c ? b : a;
<a name="line78"></a>  var actual = opt_c ? opt_c : b;
<a name="line79"></a>
<a name="line80"></a>  assertEquals(message + &#39;Array lengths unequal.&#39;,
<a name="line81"></a>               expected.length, actual.length);
<a name="line82"></a>
<a name="line83"></a>  for (var i = 0; i &lt; expected.length; i++) {
<a name="line84"></a>    assertRangesEqual(message + &#39;Range &#39; + i + &#39; mismatch.&#39;,
<a name="line85"></a>                      expected[i], actual[i]);
<a name="line86"></a>  }
<a name="line87"></a>}
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>function testClone() {
<a name="line91"></a>  var r = new goog.math.RangeSet();
<a name="line92"></a>
<a name="line93"></a>  var test = new goog.math.RangeSet(r);
<a name="line94"></a>  assertRangeListsEqual([], test.ranges_);
<a name="line95"></a>
<a name="line96"></a>  r.add(new goog.math.Range(-10, -2));
<a name="line97"></a>  r.add(new goog.math.Range(2.72, 3.14));
<a name="line98"></a>  r.add(new goog.math.Range(8, 11));
<a name="line99"></a>
<a name="line100"></a>  test = r.clone();
<a name="line101"></a>  assertRangeListsEqual([[-10, -2], [2.72, 3.14], [8, 11]], test.ranges_);
<a name="line102"></a>
<a name="line103"></a>  var test2 = r.clone();
<a name="line104"></a>  assertRangeListsEqual(test.ranges_, test2.ranges_);
<a name="line105"></a>
<a name="line106"></a>  assertNotEquals(&#39;The clones should not share the same list reference.&#39;,
<a name="line107"></a>                  test.ranges_, test2.ranges_);
<a name="line108"></a>
<a name="line109"></a>  for (var i = 0; i &lt; test.ranges_.length; i++) {
<a name="line110"></a>    assertNotEquals(&#39;The clones should not share references to ranges.&#39;,
<a name="line111"></a>                    test.ranges_[i], test2.ranges_[i]);
<a name="line112"></a>  }
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>function testAddNoCorruption() {
<a name="line117"></a>  var r = new goog.math.RangeSet();
<a name="line118"></a>
<a name="line119"></a>  var range = new goog.math.Range(1, 2);
<a name="line120"></a>  r.add(range);
<a name="line121"></a>
<a name="line122"></a>  assertNotEquals(&#39;Only a copy of the input range should be stored.&#39;,
<a name="line123"></a>                  range, r.ranges_[0]);
<a name="line124"></a>
<a name="line125"></a>  range.end = 5;
<a name="line126"></a>  assertRangeListsEqual(&#39;Modifying an input range after use should not &#39; +
<a name="line127"></a>                        &#39;affect the set.&#39;,
<a name="line128"></a>                        [[1, 2]], r.ranges_);
<a name="line129"></a>}
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>function testAdd() {
<a name="line133"></a>  var r = new goog.math.RangeSet();
<a name="line134"></a>
<a name="line135"></a>  r.add(new goog.math.Range(7, 12));
<a name="line136"></a>  assertRangeListsEqual([[7, 12]], r.ranges_);
<a name="line137"></a>
<a name="line138"></a>  r.add(new goog.math.Range(1, 3));
<a name="line139"></a>  assertRangeListsEqual([[1, 3], [7, 12]], r.ranges_);
<a name="line140"></a>
<a name="line141"></a>  r.add(new goog.math.Range(13, 18));
<a name="line142"></a>  assertRangeListsEqual([[1, 3], [7, 12], [13, 18]], r.ranges_);
<a name="line143"></a>
<a name="line144"></a>  r.add(new goog.math.Range(5, 5));
<a name="line145"></a>  assertRangeListsEqual(&#39;Zero length ranges should be ignored.&#39;,
<a name="line146"></a>                        [[1, 3], [7, 12], [13, 18]], r.ranges_);
<a name="line147"></a>
<a name="line148"></a>  var badRange = new goog.math.Range(5, 5);
<a name="line149"></a>  badRange.end = 4;
<a name="line150"></a>  r.add(badRange);
<a name="line151"></a>  assertRangeListsEqual(&#39;Negative length ranges should be ignored.&#39;,
<a name="line152"></a>                        [[1, 3], [7, 12], [13, 18]], r.ranges_);
<a name="line153"></a>
<a name="line154"></a>  r.add(new goog.math.Range(-22, -15));
<a name="line155"></a>  assertRangeListsEqual(&#39;Negative ranges should work fine.&#39;,
<a name="line156"></a>                        [[-22, -15], [1, 3], [7, 12], [13, 18]], r.ranges_);
<a name="line157"></a>
<a name="line158"></a>  r.add(new goog.math.Range(3.1, 6.9));
<a name="line159"></a>  assertRangeListsEqual(&#39;Non-integer ranges should work fine.&#39;,
<a name="line160"></a>                        [[-22, -15], [1, 3], [3.1, 6.9], [7, 12], [13, 18]],
<a name="line161"></a>                        r.ranges_);
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>function testAddWithOverlaps() {
<a name="line166"></a>  var r = new goog.math.RangeSet();
<a name="line167"></a>
<a name="line168"></a>  r.add(new goog.math.Range(7, 12));
<a name="line169"></a>  r.add(new goog.math.Range(5, 8));
<a name="line170"></a>  assertRangeListsEqual([[5, 12]], r.ranges_);
<a name="line171"></a>
<a name="line172"></a>  r.add(new goog.math.Range(15, 20));
<a name="line173"></a>  r.add(new goog.math.Range(18, 25));
<a name="line174"></a>  assertRangeListsEqual([[5, 12], [15, 25]], r.ranges_);
<a name="line175"></a>
<a name="line176"></a>  r.add(new goog.math.Range(10, 17));
<a name="line177"></a>  assertRangeListsEqual([[5, 25]], r.ranges_);
<a name="line178"></a>
<a name="line179"></a>  r.add(new goog.math.Range(-4, 4.5));
<a name="line180"></a>  assertRangeListsEqual([[-4, 4.5], [5, 25]], r.ranges_);
<a name="line181"></a>
<a name="line182"></a>  r.add(new goog.math.Range(4.2, 5.3));
<a name="line183"></a>  assertRangeListsEqual([[-4, 25]], r.ranges_);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>function testAddWithAdjacentSpans() {
<a name="line188"></a>  var r = new goog.math.RangeSet();
<a name="line189"></a>
<a name="line190"></a>  r.add(new goog.math.Range(7, 12));
<a name="line191"></a>  r.add(new goog.math.Range(13, 19));
<a name="line192"></a>  assertRangeListsEqual([[7, 12], [13, 19]], r.ranges_);
<a name="line193"></a>
<a name="line194"></a>  r.add(new goog.math.Range(4, 6));
<a name="line195"></a>  assertRangeListsEqual([[4, 6], [7, 12], [13, 19]], r.ranges_);
<a name="line196"></a>
<a name="line197"></a>  r.add(new goog.math.Range(6, 7));
<a name="line198"></a>  assertRangeListsEqual([[4, 12], [13, 19]], r.ranges_);
<a name="line199"></a>
<a name="line200"></a>  r.add(new goog.math.Range(12, 13));
<a name="line201"></a>  assertRangeListsEqual([[4, 19]], r.ranges_);
<a name="line202"></a>
<a name="line203"></a>  r.add(new goog.math.Range(19.1, 22));
<a name="line204"></a>  assertRangeListsEqual([[4, 19], [19.1, 22]], r.ranges_);
<a name="line205"></a>
<a name="line206"></a>  r.add(new goog.math.Range(19, 19.1));
<a name="line207"></a>  assertRangeListsEqual([[4, 22]], r.ranges_);
<a name="line208"></a>
<a name="line209"></a>  r.add(new goog.math.Range(-3, -2));
<a name="line210"></a>  assertRangeListsEqual([[-3, -2], [4, 22]], r.ranges_);
<a name="line211"></a>
<a name="line212"></a>  r.add(new goog.math.Range(-2, 4));
<a name="line213"></a>  assertRangeListsEqual([[-3, 22]], r.ranges_);
<a name="line214"></a>}
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>function testAddWithSubsets() {
<a name="line218"></a>  var r = new goog.math.RangeSet();
<a name="line219"></a>
<a name="line220"></a>  r.add(new goog.math.Range(7, 12));
<a name="line221"></a>  assertRangeListsEqual([[7, 12]], r.ranges_);
<a name="line222"></a>
<a name="line223"></a>  r.add(new goog.math.Range(7, 12));
<a name="line224"></a>  assertRangeListsEqual([[7, 12]], r.ranges_);
<a name="line225"></a>
<a name="line226"></a>  r.add(new goog.math.Range(8, 11));
<a name="line227"></a>  assertRangeListsEqual([[7, 12]], r.ranges_);
<a name="line228"></a>
<a name="line229"></a>  for (var i = 20; i &lt; 30; i += 2) {
<a name="line230"></a>    r.add(new goog.math.Range(i, i + 1));
<a name="line231"></a>  }
<a name="line232"></a>  assertRangeListsEqual(
<a name="line233"></a>      [[7, 12], [20, 21], [22, 23], [24, 25], [26, 27], [28, 29]],
<a name="line234"></a>      r.ranges_);
<a name="line235"></a>
<a name="line236"></a>  r.add(new goog.math.Range(1, 30));
<a name="line237"></a>  assertRangeListsEqual([[1, 30]], r.ranges_);
<a name="line238"></a>}
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>function testRemove() {
<a name="line242"></a>  var r = new goog.math.RangeSet();
<a name="line243"></a>
<a name="line244"></a>  r.add(new goog.math.Range(1, 3));
<a name="line245"></a>  r.add(new goog.math.Range(7, 8));
<a name="line246"></a>  r.add(new goog.math.Range(10, 20));
<a name="line247"></a>
<a name="line248"></a>  r.remove(new goog.math.Range(3, 6));
<a name="line249"></a>  assertRangeListsEqual([[1, 3], [7, 8], [10, 20]], r.ranges_);
<a name="line250"></a>
<a name="line251"></a>  r.remove(new goog.math.Range(7, 8));
<a name="line252"></a>  assertRangeListsEqual([[1, 3], [10, 20]], r.ranges_);
<a name="line253"></a>
<a name="line254"></a>  r.remove(new goog.math.Range(1, 3));
<a name="line255"></a>  assertRangeListsEqual([[10, 20]], r.ranges_);
<a name="line256"></a>
<a name="line257"></a>  r.remove(new goog.math.Range(8, 11));
<a name="line258"></a>  assertRangeListsEqual([[11, 20]], r.ranges_);
<a name="line259"></a>
<a name="line260"></a>  r.remove(new goog.math.Range(18, 25));
<a name="line261"></a>  assertRangeListsEqual([[11, 18]], r.ranges_);
<a name="line262"></a>
<a name="line263"></a>  r.remove(new goog.math.Range(15, 16));
<a name="line264"></a>  assertRangeListsEqual([[11, 15], [16, 18]], r.ranges_);
<a name="line265"></a>
<a name="line266"></a>  r.remove(new goog.math.Range(11, 15));
<a name="line267"></a>  assertRangeListsEqual([[16, 18]], r.ranges_);
<a name="line268"></a>
<a name="line269"></a>  r.remove(new goog.math.Range(16, 16));
<a name="line270"></a>  assertRangeListsEqual(&#39;Empty ranges should be ignored.&#39;,
<a name="line271"></a>                        [[16, 18]], r.ranges_);
<a name="line272"></a>
<a name="line273"></a>  r.remove(new goog.math.Range(16, 17));
<a name="line274"></a>  assertRangeListsEqual([[17, 18]], r.ranges_);
<a name="line275"></a>
<a name="line276"></a>  r.remove(new goog.math.Range(17, 18));
<a name="line277"></a>  assertRangeListsEqual([], r.ranges_);
<a name="line278"></a>}
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>function testRemoveWithNonOverlappingRanges() {
<a name="line282"></a>  var r = new goog.math.RangeSet();
<a name="line283"></a>
<a name="line284"></a>  r.add(new goog.math.Range(10, 20));
<a name="line285"></a>
<a name="line286"></a>  r.remove(new goog.math.Range(5, 8));
<a name="line287"></a>  assertRangeListsEqual(&#39;Non-overlapping ranges should be ignored.&#39;,
<a name="line288"></a>                        [[10, 20]], r.ranges_);
<a name="line289"></a>
<a name="line290"></a>  r.remove(new goog.math.Range(20, 30));
<a name="line291"></a>  assertRangeListsEqual(&#39;Non-overlapping ranges should be ignored.&#39;,
<a name="line292"></a>                        [[10, 20]], r.ranges_);
<a name="line293"></a>
<a name="line294"></a>  r.remove(new goog.math.Range(15, 15));
<a name="line295"></a>  assertRangeListsEqual(&#39;Zero-length ranges should be ignored.&#39;,
<a name="line296"></a>                        [[10, 20]], r.ranges_);
<a name="line297"></a>}
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>function testRemoveWithIdenticalRanges() {
<a name="line301"></a>  var r = new goog.math.RangeSet();
<a name="line302"></a>
<a name="line303"></a>  r.add(new goog.math.Range(10, 20));
<a name="line304"></a>  r.add(new goog.math.Range(30, 40));
<a name="line305"></a>  r.add(new goog.math.Range(50, 60));
<a name="line306"></a>  assertRangeListsEqual([[10, 20], [30, 40], [50, 60]], r.ranges_);
<a name="line307"></a>
<a name="line308"></a>  r.remove(new goog.math.Range(30, 40));
<a name="line309"></a>  assertRangeListsEqual([[10, 20], [50, 60]], r.ranges_);
<a name="line310"></a>
<a name="line311"></a>  r.remove(new goog.math.Range(50, 60));
<a name="line312"></a>  assertRangeListsEqual([[10, 20]], r.ranges_);
<a name="line313"></a>
<a name="line314"></a>  r.remove(new goog.math.Range(10, 20));
<a name="line315"></a>  assertRangeListsEqual([], r.ranges_);
<a name="line316"></a>}
<a name="line317"></a>
<a name="line318"></a>
<a name="line319"></a>function testRemoveWithOverlappingSubsets() {
<a name="line320"></a>  var r = new goog.math.RangeSet();
<a name="line321"></a>
<a name="line322"></a>  r.add(new goog.math.Range(1, 10));
<a name="line323"></a>
<a name="line324"></a>  r.remove(new goog.math.Range(1, 4));
<a name="line325"></a>  assertRangeListsEqual([[4, 10]], r.ranges_);
<a name="line326"></a>
<a name="line327"></a>  r.remove(new goog.math.Range(8, 10));
<a name="line328"></a>  assertRangeListsEqual([[4, 8]], r.ranges_);
<a name="line329"></a>}
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>function testRemoveMultiple() {
<a name="line333"></a>  var r = new goog.math.RangeSet();
<a name="line334"></a>
<a name="line335"></a>  r.add(new goog.math.Range(5, 8));
<a name="line336"></a>  r.add(new goog.math.Range(10, 20));
<a name="line337"></a>  r.add(new goog.math.Range(30, 35));
<a name="line338"></a>
<a name="line339"></a>  for (var i = 20; i &lt; 30; i += 2) {
<a name="line340"></a>    r.add(new goog.math.Range(i, i + 1));
<a name="line341"></a>  }
<a name="line342"></a>
<a name="line343"></a>  assertRangeListsEqual(
<a name="line344"></a>      &#39;Setting up the test data seems to have failed, how embarrassing.&#39;,
<a name="line345"></a>      [[5, 8], [10, 21], [22, 23], [24, 25], [26, 27], [28, 29], [30, 35]],
<a name="line346"></a>      r.ranges_);
<a name="line347"></a>
<a name="line348"></a>  r.remove(new goog.math.Range(15, 32));
<a name="line349"></a>  assertRangeListsEqual([[5, 8], [10, 15], [32, 35]],
<a name="line350"></a>                        r.ranges_);
<a name="line351"></a>}
<a name="line352"></a>
<a name="line353"></a>
<a name="line354"></a>function testRemoveWithRealNumbers() {
<a name="line355"></a>  var r = new goog.math.RangeSet();
<a name="line356"></a>
<a name="line357"></a>  r.add(new goog.math.Range(2, 4));
<a name="line358"></a>
<a name="line359"></a>  r.remove(new goog.math.Range(1.1, 2.72));
<a name="line360"></a>  assertRangeListsEqual([[2.72, 4]], r.ranges_);
<a name="line361"></a>
<a name="line362"></a>  r.remove(new goog.math.Range(3.14, 5));
<a name="line363"></a>  assertRangeListsEqual([[2.72, 3.14]], r.ranges_);
<a name="line364"></a>
<a name="line365"></a>  r.remove(new goog.math.Range(2.8, 3));
<a name="line366"></a>  assertRangeListsEqual([[2.72, 2.8], [3, 3.14]], r.ranges_);
<a name="line367"></a>}
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>function testEquals() {
<a name="line371"></a>  var a = new goog.math.RangeSet();
<a name="line372"></a>  var b = new goog.math.RangeSet();
<a name="line373"></a>
<a name="line374"></a>  assertTrue(goog.math.RangeSet.equals(a, b));
<a name="line375"></a>
<a name="line376"></a>  a.add(new goog.math.Range(3, 9));
<a name="line377"></a>  assertFalse(goog.math.RangeSet.equals(a, b));
<a name="line378"></a>
<a name="line379"></a>  b.add(new goog.math.Range(4, 9));
<a name="line380"></a>  assertFalse(goog.math.RangeSet.equals(a, b));
<a name="line381"></a>
<a name="line382"></a>  b.add(new goog.math.Range(3, 4));
<a name="line383"></a>  assertTrue(goog.math.RangeSet.equals(a, b));
<a name="line384"></a>
<a name="line385"></a>  a.add(new goog.math.Range(12, 14));
<a name="line386"></a>  b.add(new goog.math.Range(11, 14));
<a name="line387"></a>  assertFalse(goog.math.RangeSet.equals(a, b));
<a name="line388"></a>
<a name="line389"></a>  a.add(new goog.math.Range(11, 12));
<a name="line390"></a>  assertTrue(goog.math.RangeSet.equals(a, b));
<a name="line391"></a>}
<a name="line392"></a>
<a name="line393"></a>
<a name="line394"></a>function testContains() {
<a name="line395"></a>  var r = new goog.math.RangeSet();
<a name="line396"></a>
<a name="line397"></a>  assertFalse(r.contains(7, 9));
<a name="line398"></a>
<a name="line399"></a>  r.add(new goog.math.Range(5, 6));
<a name="line400"></a>  r.add(new goog.math.Range(10, 20));
<a name="line401"></a>
<a name="line402"></a>  assertFalse(r.contains(new goog.math.Range(7, 9)));
<a name="line403"></a>  assertFalse(r.contains(new goog.math.Range(9, 11)));
<a name="line404"></a>  assertFalse(r.contains(new goog.math.Range(18, 22)));
<a name="line405"></a>
<a name="line406"></a>  assertTrue(r.contains(new goog.math.Range(17, 19)));
<a name="line407"></a>  assertTrue(r.contains(new goog.math.Range(5, 6)));
<a name="line408"></a>
<a name="line409"></a>  assertTrue(r.contains(new goog.math.Range(5.9, 5.999)));
<a name="line410"></a>
<a name="line411"></a>  assertFalse(&#39;An empty input range should always return false.&#39;,
<a name="line412"></a>              r.contains(new goog.math.Range(15, 15)));
<a name="line413"></a>
<a name="line414"></a>  var badRange = new goog.math.Range(15, 15);
<a name="line415"></a>  badRange.end = 14;
<a name="line416"></a>  assertFalse(&#39;An invalid range should always return false.&#39;,
<a name="line417"></a>              r.contains(badRange));
<a name="line418"></a>}
<a name="line419"></a>
<a name="line420"></a>
<a name="line421"></a>function testContainsValue() {
<a name="line422"></a>  var r = new goog.math.RangeSet();
<a name="line423"></a>
<a name="line424"></a>  assertFalse(r.containsValue(5));
<a name="line425"></a>
<a name="line426"></a>  r.add(new goog.math.Range(1, 4));
<a name="line427"></a>  r.add(new goog.math.Range(10, 20));
<a name="line428"></a>
<a name="line429"></a>  assertFalse(r.containsValue(0));
<a name="line430"></a>  assertFalse(r.containsValue(0.999));
<a name="line431"></a>  assertFalse(r.containsValue(5));
<a name="line432"></a>  assertFalse(r.containsValue(25));
<a name="line433"></a>  assertFalse(r.containsValue(20));
<a name="line434"></a>
<a name="line435"></a>  assertTrue(r.containsValue(3));
<a name="line436"></a>  assertTrue(r.containsValue(10));
<a name="line437"></a>  assertTrue(r.containsValue(19));
<a name="line438"></a>  assertTrue(r.containsValue(19.999));
<a name="line439"></a>}
<a name="line440"></a>
<a name="line441"></a>
<a name="line442"></a>function testUnion() {
<a name="line443"></a>  var a = new goog.math.RangeSet();
<a name="line444"></a>
<a name="line445"></a>  a.add(new goog.math.Range(1, 5));
<a name="line446"></a>  a.add(new goog.math.Range(10, 11));
<a name="line447"></a>  a.add(new goog.math.Range(15, 20));
<a name="line448"></a>
<a name="line449"></a>  var b = new goog.math.RangeSet();
<a name="line450"></a>
<a name="line451"></a>  b.add(new goog.math.Range(0, 5));
<a name="line452"></a>  b.add(new goog.math.Range(8, 18));
<a name="line453"></a>
<a name="line454"></a>  var test = a.union(b);
<a name="line455"></a>  assertRangeListsEqual([[0, 5], [8, 20]], test.ranges_);
<a name="line456"></a>
<a name="line457"></a>  var test = b.union(a);
<a name="line458"></a>  assertRangeListsEqual([[0, 5], [8, 20]], test.ranges_);
<a name="line459"></a>
<a name="line460"></a>  var test = a.union(a);
<a name="line461"></a>  assertRangeListsEqual(a.ranges_, test.ranges_);
<a name="line462"></a>}
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>function testDifference() {
<a name="line466"></a>  var a = new goog.math.RangeSet();
<a name="line467"></a>
<a name="line468"></a>  a.add(new goog.math.Range(1, 5));
<a name="line469"></a>  a.add(new goog.math.Range(10, 11));
<a name="line470"></a>  a.add(new goog.math.Range(15, 20));
<a name="line471"></a>
<a name="line472"></a>  var b = new goog.math.RangeSet();
<a name="line473"></a>
<a name="line474"></a>  b.add(new goog.math.Range(0, 5));
<a name="line475"></a>  b.add(new goog.math.Range(8, 18));
<a name="line476"></a>
<a name="line477"></a>  var test = a.difference(b);
<a name="line478"></a>  assertRangeListsEqual([[18, 20]], test.ranges_);
<a name="line479"></a>
<a name="line480"></a>  var test = b.difference(a);
<a name="line481"></a>  assertRangeListsEqual([[0, 1], [8, 10], [11, 15]], test.ranges_);
<a name="line482"></a>
<a name="line483"></a>  var test = a.difference(a);
<a name="line484"></a>  assertRangeListsEqual([], test.ranges_);
<a name="line485"></a>
<a name="line486"></a>  var test = b.difference(b);
<a name="line487"></a>  assertRangeListsEqual([], test.ranges_);
<a name="line488"></a>}
<a name="line489"></a>
<a name="line490"></a>
<a name="line491"></a>function testIntersection() {
<a name="line492"></a>  var a = new goog.math.RangeSet();
<a name="line493"></a>
<a name="line494"></a>  a.add(new goog.math.Range(1, 5));
<a name="line495"></a>  a.add(new goog.math.Range(10, 11));
<a name="line496"></a>  a.add(new goog.math.Range(15, 20));
<a name="line497"></a>
<a name="line498"></a>  var b = new goog.math.RangeSet();
<a name="line499"></a>
<a name="line500"></a>  b.add(new goog.math.Range(0, 5));
<a name="line501"></a>  b.add(new goog.math.Range(8, 18));
<a name="line502"></a>
<a name="line503"></a>  var test = a.intersection(b);
<a name="line504"></a>  assertRangeListsEqual([[1, 5], [10, 11], [15, 18]], test.ranges_);
<a name="line505"></a>
<a name="line506"></a>  var test = b.intersection(a);
<a name="line507"></a>  assertRangeListsEqual([[1, 5], [10, 11], [15, 18]], test.ranges_);
<a name="line508"></a>
<a name="line509"></a>  var test = a.intersection(a);
<a name="line510"></a>  assertRangeListsEqual(a.ranges_, test.ranges_);
<a name="line511"></a>}
<a name="line512"></a>
<a name="line513"></a>
<a name="line514"></a>function testSlice() {
<a name="line515"></a>  var r = new goog.math.RangeSet();
<a name="line516"></a>
<a name="line517"></a>  r.add(new goog.math.Range(2, 4));
<a name="line518"></a>  r.add(new goog.math.Range(5, 6));
<a name="line519"></a>  r.add(new goog.math.Range(9, 15));
<a name="line520"></a>
<a name="line521"></a>  var test = r.slice(new goog.math.Range(0, 2));
<a name="line522"></a>  assertRangeListsEqual([], test.ranges_);
<a name="line523"></a>
<a name="line524"></a>  test = r.slice(new goog.math.Range(2, 4));
<a name="line525"></a>  assertRangeListsEqual([[2, 4]], test.ranges_);
<a name="line526"></a>
<a name="line527"></a>  test = r.slice(new goog.math.Range(7, 20));
<a name="line528"></a>  assertRangeListsEqual([[9, 15]], test.ranges_);
<a name="line529"></a>
<a name="line530"></a>  test = r.slice(new goog.math.Range(4, 30));
<a name="line531"></a>  assertRangeListsEqual([[5, 6], [9, 15]], test.ranges_);
<a name="line532"></a>
<a name="line533"></a>  test = r.slice(new goog.math.Range(2, 15));
<a name="line534"></a>  assertRangeListsEqual([[2, 4], [5, 6], [9, 15]], test.ranges_);
<a name="line535"></a>
<a name="line536"></a>  test = r.slice(new goog.math.Range(10, 10));
<a name="line537"></a>  assertRangeListsEqual(&#39;An empty range should produce an empty set.&#39;,
<a name="line538"></a>                        [], test.ranges_);
<a name="line539"></a>
<a name="line540"></a>  var badRange = new goog.math.Range(10, 10);
<a name="line541"></a>  badRange.end = 9;
<a name="line542"></a>  test = r.slice(badRange);
<a name="line543"></a>  assertRangeListsEqual(&#39;An invalid range should produce an empty set.&#39;,
<a name="line544"></a>                        [], test.ranges_);
<a name="line545"></a>}
<a name="line546"></a>
<a name="line547"></a>
<a name="line548"></a>function testInverse() {
<a name="line549"></a>  var r = new goog.math.RangeSet();
<a name="line550"></a>
<a name="line551"></a>  r.add(new goog.math.Range(1, 3));
<a name="line552"></a>  r.add(new goog.math.Range(5, 6));
<a name="line553"></a>  r.add(new goog.math.Range(8, 10));
<a name="line554"></a>
<a name="line555"></a>  var test = r.inverse(new goog.math.Range(10, 20));
<a name="line556"></a>  assertRangeListsEqual([[10, 20]], test.ranges_);
<a name="line557"></a>
<a name="line558"></a>  test = r.inverse(new goog.math.Range(1, 3));
<a name="line559"></a>  assertRangeListsEqual([], test.ranges_);
<a name="line560"></a>
<a name="line561"></a>  test = r.inverse(new goog.math.Range(0, 2));
<a name="line562"></a>  assertRangeListsEqual([[0, 1]], test.ranges_);
<a name="line563"></a>
<a name="line564"></a>  test = r.inverse(new goog.math.Range(9, 12));
<a name="line565"></a>  assertRangeListsEqual([[10, 12]], test.ranges_);
<a name="line566"></a>
<a name="line567"></a>  test = r.inverse(new goog.math.Range(2, 9));
<a name="line568"></a>  assertRangeListsEqual([[3, 5], [6, 8]], test.ranges_);
<a name="line569"></a>
<a name="line570"></a>  test = r.inverse(new goog.math.Range(4, 9));
<a name="line571"></a>  assertRangeListsEqual([[4, 5], [6, 8]], test.ranges_);
<a name="line572"></a>
<a name="line573"></a>  test = r.inverse(new goog.math.Range(9, 9));
<a name="line574"></a>  assertRangeListsEqual(&#39;An empty range should produce an empty set.&#39;,
<a name="line575"></a>                        [], test.ranges_);
<a name="line576"></a>
<a name="line577"></a>  var badRange = new goog.math.Range(9, 9);
<a name="line578"></a>  badRange.end = 8;
<a name="line579"></a>  test = r.inverse(badRange);
<a name="line580"></a>  assertRangeListsEqual(&#39;An invalid range should produce an empty set.&#39;,
<a name="line581"></a>                        [], test.ranges_);
<a name="line582"></a>}
<a name="line583"></a>
<a name="line584"></a>
<a name="line585"></a>function testCoveredLength() {
<a name="line586"></a>  var r = new goog.math.RangeSet();
<a name="line587"></a>  assertEquals(0, r.coveredLength());
<a name="line588"></a>
<a name="line589"></a>  r.add(new goog.math.Range(5, 9));
<a name="line590"></a>  assertEquals(4, r.coveredLength());
<a name="line591"></a>
<a name="line592"></a>  r.add(new goog.math.Range(0, 3));
<a name="line593"></a>  r.add(new goog.math.Range(12, 13));
<a name="line594"></a>  assertEquals(8, r.coveredLength());
<a name="line595"></a>
<a name="line596"></a>  r.add(new goog.math.Range(-1, 13));
<a name="line597"></a>  assertEquals(14, r.coveredLength());
<a name="line598"></a>
<a name="line599"></a>  r.add(new goog.math.Range(13, 13.5));
<a name="line600"></a>  assertEquals(14.5, r.coveredLength());
<a name="line601"></a>}
<a name="line602"></a>
<a name="line603"></a>
<a name="line604"></a>function testGetBounds() {
<a name="line605"></a>  var r = new goog.math.RangeSet();
<a name="line606"></a>
<a name="line607"></a>  assertNull(r.getBounds());
<a name="line608"></a>
<a name="line609"></a>  r.add(new goog.math.Range(12, 54));
<a name="line610"></a>  assertRangesEqual([12, 54], r.getBounds());
<a name="line611"></a>
<a name="line612"></a>  r.add(new goog.math.Range(108, 139));
<a name="line613"></a>  assertRangesEqual([12, 139], r.getBounds());
<a name="line614"></a>}
<a name="line615"></a>
<a name="line616"></a>
<a name="line617"></a>function testIsEmpty() {
<a name="line618"></a>  var r = new goog.math.RangeSet();
<a name="line619"></a>  assertTrue(r.isEmpty());
<a name="line620"></a>
<a name="line621"></a>  r.add(new goog.math.Range(0, 1));
<a name="line622"></a>  assertFalse(r.isEmpty());
<a name="line623"></a>
<a name="line624"></a>  r.remove(new goog.math.Range(0, 1));
<a name="line625"></a>  assertTrue(r.isEmpty());
<a name="line626"></a>}
<a name="line627"></a>
<a name="line628"></a>
<a name="line629"></a>function testClear() {
<a name="line630"></a>  var r = new goog.math.RangeSet();
<a name="line631"></a>
<a name="line632"></a>  r.add(new goog.math.Range(1, 2));
<a name="line633"></a>  r.add(new goog.math.Range(3, 5));
<a name="line634"></a>  r.add(new goog.math.Range(8, 13));
<a name="line635"></a>
<a name="line636"></a>  assertFalse(r.isEmpty());
<a name="line637"></a>
<a name="line638"></a>  r.clear();
<a name="line639"></a>  assertTrue(r.isEmpty());
<a name="line640"></a>}
<a name="line641"></a>
<a name="line642"></a>
<a name="line643"></a>function testIter() {
<a name="line644"></a>  var r = new goog.math.RangeSet();
<a name="line645"></a>
<a name="line646"></a>  r.add(new goog.math.Range(1, 3));
<a name="line647"></a>  r.add(new goog.math.Range(5, 6));
<a name="line648"></a>  r.add(new goog.math.Range(8, 10));
<a name="line649"></a>
<a name="line650"></a>  assertRangeListsEqual([[1, 3], [5, 6], [8, 10]], goog.iter.toArray(r));
<a name="line651"></a>
<a name="line652"></a>  var i = 0;
<a name="line653"></a>  goog.iter.forEach(r, function(testRange) {
<a name="line654"></a>    assertRangesEqual(&#39;Iterated set values should match the originals.&#39;,
<a name="line655"></a>                      r.ranges_[i], testRange);
<a name="line656"></a>    assertNotEquals(&#39;Iterated range should not be a reference to the original.&#39;,
<a name="line657"></a>                    r.ranges_[i], testRange);
<a name="line658"></a>    i++;
<a name="line659"></a>  });
<a name="line660"></a>}
</pre>


</body>
</html>
